//
//  AddAddressViewModel.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/25.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class AddAddressViewModel: BaseViewModel {
    var address:String = ""
    var remark:String = ""
    var coinCode:String = ""
    
    var codeNumber = "" //验证码
    
    lazy var addressRequest: AddAddressRequest = {
        let api = AddAddressRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    lazy var checkAddress:CheckAddressRequest = {
        let api = CheckAddressRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    /// 校验地址是否正确
    ///
    /// - Parameter returnVale: 回调
    func checkCoinAddress(returnVale:@escaping (_ canSend:Bool) -> ()) {
        self.request(request: checkAddress, success: { (result) in
            returnVale(true)
        }) { (response) in
            CYMBShow.showMessage(response.errorMsg)
            returnVale(false)
        }
        
    }
    
    func addAddress() {
        self.request(request: addressRequest, success: {[weak self] (result) in
            NotificationCenter.default.post(name: NSNotification.Name.Exchange.addressChange, object: nil)
            NotificationCenter.default.post(name: NSNotification.Name.Exchange.addressNumberChange, object: nil)
            CYMBShow.showMessage(kText("添加提币地址成功"))
            self?.controller?.navigationController?.popViewController(animated: true)
        }) { (response) in
            CYMBShow.showMessage(response.errorMsg.count != 0 ? response.errorMsg : "")
        }
    }
    
    func archiveStates() {
        AppSecurityInfo.default.archiveSecurityInfo()
    }
}

extension AddAddressViewModel: RequestCallBack,RequestDataReformer,RequestParamSource {
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        let block = self.successBlockDic.object(forKey: request.path())
        if ((block as? success) != nil) {
            (block as! success)(data)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        
    }
    
    func requestDidFail(_ request: BaseRequest) {
        let block = self.failBlockDic.object(forKey: request.path())
        if ((block as? fail) != nil) {
            (block as! fail)(request)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? AddAddressRequest {
            pramas.updateValue(10, forKey: "codeType")
            pramas.updateValue(coinCode, forKey: "coinCode")
            pramas.updateValue(address, forKey: "drawAdd")
            pramas.updateValue(remark, forKey: "remarks")
            if AppSecurityInfo.default.model.isOpenGoogle {
                pramas.updateValue(codeNumber, forKey: "googleCode")
                pramas.updateValue(2, forKey: "type")
            }else if AppSecurityInfo.default.model.isMobileBind {
                pramas.updateValue(codeNumber, forKey: "mobileCode")
                pramas.updateValue(AppSecurityInfo.default.model.mobile, forKey: "mobile")
                pramas.updateValue(0, forKey: "type")
            }else {
                
            }
        }else if let _ = request as? CheckAddressRequest {
            pramas.updateValue(address, forKey: "drawAdd")
            pramas.updateValue(coinCode, forKey: "coinCode")
        }
        return pramas
    }
    
    
}
